﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Fotobanka
{
    public partial class UkazFoto1 : System.Web.UI.Page
    {

        private int photo_id = 0;
        CommentsManager cm;
        RatingsManager rm;
        String hash;

        protected void Page_Load(object sender, EventArgs e)
        {
            cm = new CommentsManager();
            rm = new RatingsManager();

            hash = Request.Params["h"];
            String photo_id_string = Request.Params["pid"];
            String del = Request.Params["cd"];

            //pokud url obsahuje parametr pro smazani komentare
            if (del != null && !del.Equals(""))
            {
                try
                {
                    int id = Convert.ToInt32(del);
                    cm.deleteComment(id);
                    Response.Redirect("UkazFoto.aspx?h="+hash+"&pid="+photo_id_string);
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message;
                }
            }

            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                Image1.ImageUrl = "~/" + Fotobanka.Utils.FotoFolder + "/" + hash;
                try
                {
                    photo_id = Convert.ToInt32(photo_id_string.Trim());
                    komentare_rep.DataSource = cm.SelectCommentsForPhoto(photo_id, li.Role, li.Id, hash);
                    komentare_rep.DataBind();

                    hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message;
                    photo_id = 0;
                }
            }
            else
            {
                Label1.Text = "Neprihlaseny uzivatel!";
            }

        }


        protected void komentar_vlozit_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                String komentar = komentar_novy.Text;
                if (photo_id > 0 && komentar.Length > 0)
                {
                    CommentsObject c = new CommentsObject();
                    c.Photo_id = photo_id;
                    c.User_id = li.Id;
                    c.Text = komentar;
                    cm.InsertComment(c);
                    Label1.Text = "Komentar byl uspesne pridan";
                    komentar_novy.Text = "";
                    komentare_rep.DataSource = cm.SelectCommentsForPhoto(photo_id, li.Role, li.Id, hash);
                    komentare_rep.DataBind();
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                rm = new RatingsManager();
                rm.InsertRating(photo_id, li.Id, 1);
                hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                rm = new RatingsManager();
                rm.InsertRating(photo_id, li.Id, 2);
                hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                rm = new RatingsManager();
                rm.InsertRating(photo_id, li.Id, 3);
                hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                rm = new RatingsManager();
                rm.InsertRating(photo_id, li.Id, 4);
                hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            LoginInfo li = (LoginInfo)HttpContext.Current.Session["logged_user_id"];
            if (li != null)
            {
                rm = new RatingsManager();
                rm.InsertRating(photo_id, li.Id, 5);
                hodnoceni.Text = rm.SelectRatingForPhoto(photo_id).ToString();
            }
        }

    }
}
